package dpreview;

import java.io.*;

import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Vector;
import org.jdom.*;
import org.jdom.output.*;

public class CameraToXML {

    private Vector<Camera> Cameras;
    private String brand;

    public CameraToXML(String brand) {
        //System.out.println(">> "+brand);
        Cameras = new Vector<Camera>();
        this.brand = brand;
    }

    public void addCamera(Camera p) {
        this.Cameras.add(p);
    }

    public void generateXML() throws IOException {
        Element pubElement = new Element("search");
        pubElement.setAttribute("brand", this.brand);
        Document myDocument = new Document(pubElement);

        // identifica o xml para ser tratado pelo XSLT
        HashMap<String, String> piMap = new HashMap<String, String>(2);
        piMap.put("type", "text/xsl");
        piMap.put("href", "../src/xsl/search_to_html.xsl");
        ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", piMap);
        myDocument.getContent().add(0, pi);
        // escreve XML
        for (Enumeration<Camera> e = this.Cameras.elements(); e.hasMoreElements();) {
            Camera aux = (Camera) e.nextElement();

            Element camera = new Element("camera");
            camera.setAttribute("model", aux.getModel());

            Element description = new Element("description");
            description.addContent(aux.getDescription());
            camera.addContent(description);

            Element announced = new Element("announced");
	    
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    String dateAnnounced = sdf.format(aux.getAnounced());
            announced.addContent(dateAnnounced);
            camera.addContent(announced);

            Element max_resolution = new Element("max_resolution");

            Element width = new Element("width");
            width.addContent("" + aux.getMax_res()[0]);
            max_resolution.addContent(width);

            Element height = new Element("height");
            height.addContent("" + aux.getMax_res()[1]);
            max_resolution.addContent(height);

            camera.addContent(max_resolution);

            Element lower_resolutions = new Element("lower_resolutions");

            //Parsing das N lower resolutions
            Vector<int[]> n_lr = aux.getLower_res();
            for (int i = 0; i < n_lr.size(); i++) {
                Element res = new Element("res");

                Element width2 = new Element("width");
                width2.addContent("" + n_lr.elementAt(i)[0]);
                res.addContent(width2);

                Element height2 = new Element("height");
                height2.addContent("" + n_lr.elementAt(i)[1]);
                res.addContent(height2);

                lower_resolutions.addContent(res);
            }

            camera.addContent(lower_resolutions);


            Element image_ratio = new Element("image_ratio");

            //Parsing dos N image ratios
            Vector<int[]> n_ir = aux.getImage_ratio();
            for (int i = 0; i < n_ir.size(); i++) {
                Element ratio = new Element("ratio");

                Element width2 = new Element("width");
                width2.addContent("" + n_ir.elementAt(i)[0]);
                ratio.addContent(width2);

                Element height2 = new Element("height");
                height2.addContent("" + n_ir.elementAt(i)[1]);
                ratio.addContent(height2);

                image_ratio.addContent(ratio);
            }

            camera.addContent(image_ratio);

            Element effective_pixels = new Element("effective_pixels");
            effective_pixels.addContent("" + aux.getEfective_pixels());
            camera.addContent(effective_pixels);


	    Element sensor_size = new Element("sensor_size");
	    String temp = aux.getSensor_size_width();
	    if(temp != null){
		Element sensor_width = new Element("width");
		sensor_width.addContent(temp);
		sensor_size.addContent(sensor_width);
	    }

	   
	    temp = aux.getSensor_size_height();
	    if(temp != null){
		Element sensor_height = new Element("height");
		sensor_height.addContent(temp);
		sensor_size.addContent(sensor_height);
	    }

	    temp = aux.getSensor_size_fraction();
	    if(temp != null){
		Element sensor_fraction = new Element("fraction");
		sensor_fraction.addContent(temp);
		sensor_size.addContent(sensor_fraction);
	    }

	    temp = aux.getSensor_size_area();
	    if(temp != null){
		Element sensor_area = new Element("area");
		sensor_area.addContent(temp);
		sensor_size.addContent(sensor_area);
	    }
	    
	    camera.addContent(sensor_size);

            Element iso_rating = new Element("iso_rating");
            //Parsing dos N iso ratings
            Vector<String> n_isor = aux.getISO_rating();
            for (int i = 0; i < n_isor.size(); i++) {
                Element iso = new Element("iso");
                iso.addContent(n_isor.elementAt(i));
                iso_rating.addContent(iso);
            }
            camera.addContent(iso_rating);


            Element shutter = new Element("shutter");

	    if(aux.getShutter() != null){
		Element shutter_min = new Element("min");
		shutter_min.addContent(aux.getShutter()[0]);
		shutter.addContent(shutter_min);

		Element shutter_max = new Element("max");
		shutter_max.addContent(aux.getShutter()[1]);
		shutter.addContent(shutter_max);
	    }
            camera.addContent(shutter);


            Element review_link = new Element("review_link");
            review_link.addContent(aux.getReview_link());
            camera.addContent(review_link);

            Element image_link = new Element("image_link");
            image_link.addContent(aux.getImage_link());
            camera.addContent(image_link);

            pubElement.addContent(camera);

            // Escreve para disco
            XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("output\\"+brand+".xml"), "UTF-8");

            outputter.output(myDocument, out);
            out.close();
        }
	//validateXML(myDocument);
    }
/*
    boolean validateXML(Document myDocument){

        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

	try {
	    // compila o XSD
	    File schemaLocation = new File("output\\schema.xsd");
	    Schema schema = factory.newSchema(schemaLocation);

	    // cria o objecto Validator baseado no XSD
	    Validator validator = schema.newValidator();

	    // faz o parse do documento XML criado
	   Source source = new StreamSource("output\\"+brand+".xml");

	    // valida o documento
	    validator.validate(source);
	    
            System.out.println(brand+".xml is valid.");
        }
        catch (SAXException ex) {
            System.out.println(brand+".xml is not valid because ");
            System.out.println(ex.getMessage());
        } catch (IOException ex) {
	    Logger.getLogger(CameraToXML.class.getName()).log(Level.SEVERE, null, ex);
	}
	
	return true;
    }*/
}
